<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-11-01
 * Time: 14:02
 */

namespace app\index\controller;

use think\Db;
use think\Loader;

class Myself extends Base
{
    //获取员工信息
    public function index()
    {
        $this->baseindex();
        $arr = $_SESSION['id'];
        $result = Db::table('oa_user')->where('id=' . $arr)->find();//获取员工信息
        $branch = Db::table('oa_user u')->field('b.id,b.branch')->join('oa_branch b', 'u.branch_id=b.id', 'left')->where('u.id=' . $arr)->find();//获取员工信息
        $region = Db::table('oa_user u')->field('r.id,r.region')->join('oa_region r', 'u.region_id=r.id', 'left')->where('u.id=' . $arr)->find();//获取员工信息
        //$branchs = Db::table('oa_branch')->field('id,branch')->where('region_id=' . $result['region_id'])->select();//获取部门信息
        $role = Db::table('oa_user_role u,oa_role r')->field('u.state,r.id,r.role')->where('u.role_id=r.id and u.user_id=' . $arr . ' and (r.state=1 or r.state=2)')->select();
        $month = Db::table('oa_month')->select();
        //dump($branchs);die;
        $this->assign('region', $region);
        $this->assign('role', $role);
        $this->assign('month', $month);
        $this->assign('branch', $branch);
        //$this->assign('branchs', $branchs);
        $this->assign('list', $result);
        return $this->fetch();
    }

    //修改密码
    public function savepass()
    {
        $this->baseindex();
        if (isset($_POST['sub']) && $_POST['sub'] == "确定修改") {
            $res = input('post.data');
            $result = json_decode($res, true);
            $email = $result['email'];
            $ch_name = $result['ch_name'];
            $rand = $result['rand'];
            $password = $result['password'];
            $result1 = Db::table('oa_user')->where(['ch_name' => $ch_name, 'email' => $email])->select();
            $result2 = Db::table('oa_user')->where(['en_name' => $ch_name, 'email' => $email])->select();
            $results = Db::table('oa_rand_email')->where(['rand' => $rand, 'user_email' => $email])->select();
            if (count($result1) == 1 && count($results) == 1) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    //验证与修改前密码是否一致
                    $password_equal1 = Db::table('oa_user')->where(['id' => $result1[0]['id'], 'password' => $pass])
                        ->select();
                    if (!empty($password_equal1)) {
                        return 2;
                    } else {
                        $num = Db::table('oa_user')->where(['id' => $result1[0]['id']])->update($where);
                        if ($num) {
                            return $num;
                        } else {
                            return 0;
                        }
                    }
                }
            } elseif (count($result2) == 1 && count($results) == 1) {
                if (!empty($password)) {
                    $pass = md5($password);
                    $where['password'] = $pass;
                    $password_equal2 = Db::table('oa_user')->where(['id' => $result1[0]['id'], 'password' => $pass])
                        ->select();
                    if (!empty($password_equal2)) {
                        return 2;
                    } else {
                        $num = Db::table('oa_user')->where(['id' => $result2[0]['id']])->update($where);
                        if ($num) {
                            return $num;
                        } else {
                            return 0;
                        }
                    }
                }
            } else {
                return 0;
            }
        }
        return $this->fetch();
    }

    //生成验证码并发送至邮箱
    public function get_rand()
    {
        $this->baseindex();
        if (isset($_POST['submit_a']) && $_POST['submit_a'] == "点击获取验证码") {
            $email = input('post.email_b');
            $ch_name = input('post.ch_name');
            $yan_email = $this->getEmaila($email);
            if ($yan_email) {
                $result = Db::table('oa_user')->where(["ch_name" => $ch_name, "email" => $email])->select();
                $results = Db::table('oa_user')->where(["en_name" => $ch_name, "email" => $email])->select();
                if (count($result) == 1 || count($results) == 1) {
                    $time = time();
                    $num = Db::table('oa_rand_email')->field('start_time,end_time')
                        ->where(['user_email' => $email, 'start_time' => ['<', $time], 'end_time' => ['>', $time]])->select();
                    if (empty($num)) {
                        $rand = rand(1000, 9999);
                        //return $rand;
                        $res = $this->email($email, $rand, $ch_name);
                        //return $res;
                        if ($res) {
                            $start_time = time();
                            $end_time = time() + 300;
                            $data['rand'] = $rand;
                            $data['start_time'] = $start_time;
                            $data['end_time'] = $end_time;
                            $data['user_email'] = $email;
                            $num = Db::table('oa_rand_email')->insert($data);
                            if ($num) {
                                return 1;
                            } else {
                                return 0;
                            }
                        } else {
                            return 0;
                        }
                    } else {
                        return 2;
                    }
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    //验证验证码
    public function verify_rand()
    {
        $this->baseindex();
        if (isset($_POST['submit_rand']) && $_POST['submit_rand'] == "确定") {
            $email_a = input('post.user_email');
            $ch_names = input('post.ch_name');
            $rand_a = input('post.rand');
            $starting_time = time();
            $yz_email = $this->getEmaila($email_a);
            if ($yz_email) {
                $str = Db::table('oa_user')->where(['ch_name' => $ch_names, 'email' => $email_a])->select();//验证用户名
                $str1 = Db::table('oa_user')->where(['en_name' => $ch_names, 'email' => $email_a])->select();//验证用户名拼音
                if (!empty($str) || !empty($str1)) {
                    $resultt = Db::table('oa_rand_email')->field('start_time,end_time')
                        ->where(['user_email' => $email_a, 'rand' => $rand_a, 'start_time' => ['<', $starting_time], 'end_time' => ['>', $starting_time]])->select();
                    if (!empty($resultt)) {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    //修改个人信息
    public function myselfstaff()
    {
        //form表单提交只能获取可以改变的值disable属性的值获取不到
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == 'submit') {
            $arr = input('post.data');
            $result = Db::table('oa_user')->update($arr);
            if ($result) {
                return $result;
            }
        }
    }


    /*-------------------申请-----------------*/
    //我的申请（所有）
    public function myselfapprove()
    {
        $this->baseindex();
        return $this->fetch();
    }



    /*----------------我的申请各种单据数据接口（table接口）--------------------*/

    //我的费用报销申请数据接口
    public function myselfapprove_expense_reimbursement()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            //判断用户id
            $page = input('get.page');
            $num = input('get.limit');
            $user_id = $_SESSION['id'];
            $where = 'state = 1 and user_id=' . $user_id;
            $time = input('get.reimbursement_time');
            if (isset($time) && $time != '') {
                $where .= ' and reimbursement_time=' . $time;
            }
            //取相关数据
            $res = Db::table('oa_expense_reimbursement')->field('id,type_approve,reimbursement_time,apply_state')
                ->where($where)->page($page, $num)->order('id DESC')->select();
            foreach ($res as $key => $value) {
                //数据类型（）
                $type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['type_approve']])->find();
                $res[$key]['type_approve'] = $type_approve['type_approve'];
                //审批状态（数据库存的json类型）
                $role = json_decode($value['apply_state'], true);
                $count_role_state = count($role) - 1;
                if ($role[$count_role_state]['state'] == 0) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
                }
                if ($role[$count_role_state]['state'] == 1) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">申请被审批</span>';
                }
                if ($role[$count_role_state]['state'] == 2) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">审请被驳回</span>';
                }
            }
            //数据个数
            $count = Db::table('oa_expense_reimbursement')->where('state=1 and user_id=' . $user_id)->count();
            //layui接口格式
            $data = ['code' => 0, 'msg' => '', 'count' => $count];
            $data['data'] = $res;
            return $data;
        } else {

        }
    }

    //我的付款申请数据接口
    public function myselfapprove_payment_application_form()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            //判断用户id
            $page = input('get.page');
            $num = input('get.limit');
            $user_id = $_SESSION['id'];
            $where = 'state = 1 and user_id=' . $user_id;
            $time = input('get.reimbursement_time');
            if (isset($time) && $time != '') {
                $where .= ' and reimbursement_time=' . $time;
            }
            //取相关数据
            $res = Db::table('oa_payment_application_form')->field('id,payment_application_form_time,bill_type_id,apply_state')
                ->where($where)->page($page, $num)->order('id DESC')->select();
            foreach ($res as $key => $value) {
                //数据类型（）
                $type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['bill_type_id']])->find();
                $res[$key]['type_approve'] = $type_approve['type_approve'];
                //审批状态（数据库存的json类型）
                $role = json_decode($value['apply_state'], true);
                $count_role_state = count($role) - 1;
                if ($role[$count_role_state]['state'] == 0) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
                }
                if ($role[$count_role_state]['state'] == 1) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">申请被审批</span>';
                }
                if ($role[$count_role_state]['state'] == 2) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">审批被驳回</span>';
                }
            }
            //数据个数
            $count = Db::table('oa_payment_application_form')->where('state=1 and user_id=' . $user_id)->count();
            //layui接口格式
            $data = ['code' => 0, 'msg' => '', 'count' => $count];
            $data['data'] = $res;
            return $data;
        } else {

        }
    }

    //我的请假申请数据接口
    public function myselfapprove_leaves_form()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            //判断用户id
            $page = input('get.page');
            $num = input('get.limit');
            $user_id = $_SESSION['id'];
            $where = 'state = 1 and user_id=' . $user_id;
            //取相关数据
            $res = Db::table('oa_leaves_form')->field('id,type_leave,leave_form_time,bill_type_id,apply_state')
                ->where($where)->page($page, $num)->order('id DESC')->select();
            foreach ($res as $key => $value) {
                //数据类型（）
                $type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['bill_type_id']])->find();
                $res[$key]['type_approve'] = $type_approve['type_approve'];
                //审批状态（数据库存的json类型）
                $role = json_decode($value['apply_state'], true);
                $count_role_state = count($role) - 1;
                if ($role[$count_role_state]['state'] == 0) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
                }
                if ($role[$count_role_state]['state'] == 1) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">申请被审批</span>';
                }
                if ($role[$count_role_state]['state'] == 2) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">审批被驳回</span>';
                }
            }
            //数据个数
            $count = Db::table('oa_leaves_form')->where('state=1 and user_id=' . $user_id)->count();
            //layui接口格式
            $data = ['code' => 0, 'msg' => '', 'count' => $count];
            $data['data'] = $res;
            return $data;
        } else {

        }
    }

    //我的合同申请数据接口
    public function myselfapprove_contract_form()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            //判断用户id
            $page = input('get.page');
            $num = input('get.limit');
            $user_id = $_SESSION['id'];
            $where = 'state = 1 and user_id=' . $user_id;
            //取相关数据
            $res = Db::table('oa_contract_form')->field('id,contract_type,sub_region,contract_approve_time,bill_type_id,contract_name,contract_code,apply_state')
                ->where($where)->page($page, $num)->order('id DESC')->select();
            foreach ($res as $key => $value) {
                //取单据类型
                $type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['bill_type_id']])->find();
                $res[$key]['type_approve'] = $type_approve['type_approve'];
                //取提交区域
                $type_approve = Db::table('oa_region')->field('region')->where(['id' => $value['sub_region']])->find();
                $res[$key]['sub_region'] = $type_approve['region'];
                //取合同的类型
                $type_contract = Db::table('oa_contract_type')->where(['id' => $value['contract_type']])->find();
                $res[$key]['contract_type'] = $type_contract['contract_type'];
                //审批状态（数据库存的json类型）
                $role = json_decode($value['apply_state'], true);
                $count_role_state = count($role) - 1;
                if ($role[$count_role_state]['state'] == 0) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
                }
                if ($role[$count_role_state]['state'] == 1) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">合同被审批</span>';
                }
                if ($role[$count_role_state]['state'] == 2) {
                    $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                    $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">合同被驳回</span>';
                }
            }
            //数据个数
            $count = Db::table('oa_contract_form')->where('state=1 and user_id=' . $user_id)->count();
            //layui接口格式
            $data = ['code' => 0, 'msg' => '', 'count' => $count];
            $data['data'] = $res;
            return $data;
        } else {

        }
    }

    /*-----------------（审批）-------------------*/
    //我的审批
    public function myselfapply()
    {
        $this->baseindex();
        $myselfapply_expense_reimbursement_id = '';
        $myselfapply_payment_application_form_id = '';
        $myselfapply_payment_leaves_form_id = '';
        $myselfapply_payment_contract_form_id = '';
        if (isset($_POST['submit'])) {
            $bill_type_id = input('post.bill_type_id');
            //dump($bill_type_id);
            if ($bill_type_id == 1) {
                $myselfapply_expense_reimbursement_id = input('post.expense_reimbursement_serach');
                //dump($myselfapply_expense_reimbursement_id);
            }
            if ($bill_type_id == 2) {
                $myselfapply_payment_application_form_id = input('post.payment_application_form_search');
                //dump($myselfapply_payment_application_form_id);
            }
            if ($bill_type_id == 4) {
                $myselfapply_payment_leaves_form_id = input('post.leaves_form_search');
                //dump($myselfapply_payment_application_form_id);
            }
            if ($bill_type_id == 8) {
                $myselfapply_payment_contract_form_id = input('post.contract_form_search');
                //dump($myselfapply_payment_contract_form_id);die;
            }
        }
        //付款审批数据
        $myselfapply_payment_application_form = $this->myselfapply_payment_application_form($myselfapply_payment_application_form_id);
        //费用报销审批数据
        $myselfapply_expense_reimbursement = $this->myselfapply_expense_reimbursement($myselfapply_expense_reimbursement_id);
        //请假审批数据
        $myselfapply_leaves_form = $this->myselfapply_leaves_form($myselfapply_payment_leaves_form_id);
        //合同审批数据
        $myselfapply_contract_form = $this->myselfapply_contract_form($myselfapply_payment_contract_form_id);
        /*dump($myselfapply_payment_application_form);
        dump($myselfapply_expense_reimbursement);
        dump($myselfapply_leaves_form);
        dump($myselfapply_contract_form);*/
        $this->assign('myselfapply_payment_application_form', $myselfapply_payment_application_form);
        $this->assign('myselfapply_expense_reimbursement', $myselfapply_expense_reimbursement);
        $this->assign('myselfapply_leaves_form', $myselfapply_leaves_form);
        $this->assign('myselfapply_contract_form', $myselfapply_contract_form);
        return $this->fetch();
    }



    /*-----------------审批-------------------*/

    //我的费用报销审批数据接口
    public function myselfapply_expense_reimbursement($myselfapply_expense_reimbursement_id)
    {
        //session_start();
        if (isset($_SESSION['id'])) {
            $where = "t.id=1 and t.id=e.type_approve";
            if ($myselfapply_expense_reimbursement_id != '') {
                $where .= " and e.id=" . $myselfapply_expense_reimbursement_id;
            }
            $num = 0;
            $states = '';
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            //dump($role);die;
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_expense_reimbursement e,oa_type_approve t')
                    ->field('e.id,e.reimbursement_time,e.apply_state,e.total,t.type_approve')
                    ->where($where)->order('e.id DESC')->select();
                //dump($apply_array);die;
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //dump($v);die;
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        //dump($vv);die;
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                                $num++;
                            } elseif ($vv['state'] == 1) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: black">已审批</span>';
                            } else {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: red">已驳回</span>';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['apply_state'] = $state;
                            $state_a['reimbursement_time'] = $v['reimbursement_time'];
                            $state_a['type_approve'] = $v['type_approve'];
                            $state_a['total'] = $v['total'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])) {
                                $state_a['user_id'] = $vv['user_id'];
                            } else {
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            //$states=sort($states);
            $expense_reimbursement['num'] = $num;
            $expense_reimbursement['expense_reimbursement'] = $states;
            //sort($expense_reimbursement);
            //dump($expense_reimbursement);die;
            return $expense_reimbursement;
        }
    }

    //我的付款审批数据接口
    public function myselfapply_payment_application_form($myselfapply_payment_application_form_id)
    {
        //session_start();
        if (isset($_SESSION['id'])) {
            $where = "t.id=2 and t.id=p.bill_type_id";
            if ($myselfapply_payment_application_form_id != '') {
                $where .= " and p.id=" . $myselfapply_payment_application_form_id;
            }
            $states = '';
            $num = 0;
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_payment_application_form p,oa_type_approve t')
                    ->field('p.id,p.payee,p.apply_state,p.amount_payment,p.payment_application_form_time,t.type_approve')
                    ->where($where)->order('p.id DESC')->select();
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                                $num++;
                            } elseif ($vv['state'] == 1) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: black">已审批</span>';
                            } else {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: red">已驳回</span>';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['apply_state'] = $state;
                            $state_a['payment_application_form_time'] = $v['payment_application_form_time'];
                            $state_a['type_approve'] = $v['type_approve'];
                            $state_a['amount_payment'] = $v['amount_payment'];
                            $state_a['payee'] = $v['payee'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])) {
                                $state_a['user_id'] = $vv['user_id'];
                            } else {
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            $res['num'] = $num;
            $res['myselfapply_payment_application_form'] = $states;
            return $res;
        }
    }

    //我的请假审批数据接口
    public function myselfapply_leaves_form($myselfapply_leaves_form_id)
    {
        //session_start();
        if (isset($_SESSION['id'])) {
            $where = "t.id=4 and t.id=l.bill_type_id";
            if ($myselfapply_leaves_form_id != '') {
                $where .= " and l.id=" . $myselfapply_leaves_form_id;
            }
            $num = 0;
            $states = '';
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            //dump($role);die;
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_leaves_form l,oa_type_approve t')
                    ->field('l.id,l.leave_form_time,l.type_leave,l.apply_state,t.type_approve')
                    ->where($where)->order('l.id DESC')->select();
                //dump($apply_array);die;
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //dump($v);die;
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        //dump($vv);die;
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                                $num++;
                            } elseif ($vv['state'] == 1) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: black">已审批</span>';
                            } else {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: red">已驳回</span>';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['apply_state'] = $state;
                            $state_a['leave_form_time'] = $v['leave_form_time'];
                            $state_a['type_approve'] = $v['type_approve'];
                            $state_a['type_leave'] = $v['type_leave'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])) {
                                $state_a['user_id'] = $vv['user_id'];
                            } else {
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            //$states=sort($states);
            $expense_reimbursement['num'] = $num;
            $expense_reimbursement['leaves_form'] = $states;
            //sort($expense_reimbursement);
            //dump($expense_reimbursement);die;
            return $expense_reimbursement;
        }
    }

    //我的合同审批数据接口myselfapply_contract_form
    public function myselfapply_contract_form($myselfapply_contract_form_id)
    {
        //session_start();
        if (isset($_SESSION['id'])) {
            $where = "t.id=c.contract_type and r.id=c.sub_region";
            if ($myselfapply_contract_form_id != '') {
                //return $myselfapply_contract_form_id;
                $where .= " and c.id=" . $myselfapply_contract_form_id;
            }
            $num = 0;
            $states = '';
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            //dump($role);die;
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_contract_form c,oa_region r,oa_contract_type t')
                    ->field('c.id,t.contract_type,c.contract_name,c.contract_code,c.contract_approve_time,c.apply_state,r.region')
                    ->where($where)->order('c.id DESC')->select();
                //return $apply_array;
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //dump($v);die;
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        //dump($vv);die;
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                                $num++;
                            } elseif ($vv['state'] == 1) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: black">已审批</span>';
                            } else {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: red">已驳回</span>';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['apply_state'] = $state;
                            $state_a['contract_name'] = $v['contract_name'];
                            $state_a['contract_type'] = $v['contract_type'];
                            $state_a['contract_code'] = $v['contract_code'];
                            $state_a['contract_approve_time'] = $v['contract_approve_time'];
                            $state_a['sub_region'] = $v['region'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])) {
                                $state_a['user_id'] = $vv['user_id'];
                            } else {
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            //$states=sort($states);
            foreach ($states as $k){
                $statess[]=$k['id'];
            }
            array_multisort($statess, SORT_DESC, $states);
            $contract_form['num'] = $num;
            $contract_form['contract_form'] = $states;
            //sort($expense_reimbursement);
            //dump($expense_reimbursement);die;
            return $contract_form;
        }
    }


    /*------------------审批单据详情页面及数据------------------*/

    //我的费用报销审批修改页面及数据
    public function myselfapply_expense_reimbursement_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            $results = '';
            $id = input('get.id');
            $res = Db::table('oa_expense_reimbursement')->where(['id' => $id])->find();
            //dump($res);die;
            $res['capital_total'] = $this->num_to_rmb($res['total']);
            $approve_state = json_decode($res['apply_state'], true);
            $approve_state_num = count($approve_state);
            //dump($approve_state);
            foreach ($approve_state as $key => $value) {

                $type_approve = Db::table('oa_role')->where(['id' => $value['role']])
                    ->find();
                $result_user = Db::table('oa_user')->field('ch_name')->where(['id' => $value['user_id']])
                    ->find();
                $result['role'] = $type_approve['role'];
                $result['first'] = $value['role_key'];
                $result['apply_states'] = $value['state'];
                $result['user_id'] = $result_user['ch_name'];
                if ($value['state'] == 0) {
                    $result['apply_state'] = '<span style="color: blue">待审批...</span>';
                    $result['audit_time'] = '<span style="color: blue">请等待</span>';
                    $result['audit_name'] = '<span style="color: blue">请等待...</span>';
                    $result['audit_opinion'] = '<span style="color: blue">请等待...</span>';
                }
                if ($value['state'] == 1) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    if ($key < $approve_state_num - 1) {
                        $result['apply_state'] = '已通过';
                    } else {
                        $result['apply_state'] = '已转账';
                    }
                }
                if ($value['state'] == 2) {
                    $result['audit_time'] = $value['audit_time'];
                    $result['audit_name'] =date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_opinion'] = $value['audit_opinion'];
                    $result['apply_state'] = '<span style="color: red">未通过</span>';
                }
                $results[] = $result;
            }
            $expense_reimbursement = Db::table('oa_expense_reimbursement_cost')->where(['expense_reimbursement_id' => $id])
                ->select();
            $res['expense_reimbursement_cost'] = $expense_reimbursement;
            $flow_id = Db::table('oa_flow')->where(['id' => $res['flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow_id'] = $ress;
            //dump($results);
            $this->assign('approve', $res);
            $this->assign('state', $results);
            return $this->fetch();
        }
    }

    //我的付款审批修改页面及数据
    public function myselfapply_payment_application_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            $results = '';
            $id = input('get.id');
            $res = Db::table('oa_payment_application_form')->where(['id' => $id])->find();
            //dump($res);die;
            $approve_state = json_decode($res['apply_state'], true);
            $flow = \app\index\model\Flow::get($res['payment_application_form_flow_id']);
            //审批流程的次数
            $num = 0;
            if ($flow->first != '') {
                $num++;
            }
            if ($flow->second != '') {
                $num++;
            }
            if ($flow->third != '') {
                $num++;
            }
            if ($flow->fourth != '') {
                $num++;
            }
            if ($flow->fifth != '') {
                $num++;
            }
            if ($flow->sixth != '') {
                $num++;
            }
            if ($flow->seventh != '') {
                $num++;
            }
            if ($flow->eighth != '') {
                $num++;
            }
            if ($flow->ninth != '') {
                $num++;
            }
            if ($flow->tenth != '') {
                $num++;
            }
            $approve_state_num = count($approve_state);
            //dump($approve_state);die;
            foreach ($approve_state as $key => $value) {

                $type_approve = Db::table('oa_role')->where(['id' => $value['role']])
                    ->find();
                $result_user = Db::table('oa_user')->field('ch_name')->where(['id' => $value['user_id']])
                    ->find();
                $result['role'] = $type_approve['role'];
                $result['first'] = $value['role_key'];
                $result['audit_time'] = $value['audit_time'];
                $result['audit_name'] = $value['audit_name'];
                $result['audit_opinion'] = $value['audit_opinion'];
                $result['apply_states'] = $value['state'];
                $result['user_id'] = $result_user['ch_name'];
                if ($value['state'] == 0) {
                    $result['apply_state'] = '<span style="color: blue">待审批...</span>';
                    $result['audit_time'] = '<span style="color: blue">请等待</span>';
                    $result['audit_name'] = '<span style="color: blue">请等待...</span>';
                    $result['audit_opinion'] = '<span style="color: blue">请等待...</span>';
                }
                if ($value['state'] == 1) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    if ($key < $approve_state_num - 1) {
                        $result['apply_state'] = '已通过';
                    } else {
                        $result['apply_state'] = '已转账';
                    }
                }
                if ($value['state'] == 2) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    $result['apply_state'] = '<span style="color: red">未通过</span>';
                }
                $results[] = $result;
            }
            $flow_id = Db::table('oa_flow')->where(['id' => $res['payment_application_form_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow_id'] = $ress;
            //dump($res);
            //dump($results);
            $this->assign('payment_application_form', $res);
            $this->assign('payment_application_form_state', $results);
            return $this->fetch();
        }
    }

    //我的请假审批修改页面及数据
    public function myselfapply_leaves_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            $results = '';
            $user_id = $_SESSION['id'];
            $res_list = Db::table('oa_user u,oa_branch b')->field('u.ch_name,u.position,b.branch')->where('u.branch_id=b.id and u.id=' . $user_id)
                ->find();
            $id = input('get.id');
            $res = Db::table('oa_leaves_form')->where(['id' => $id])->find();
            //dump($res);die;
            $approve_state = json_decode($res['apply_state'], true);
            foreach ($approve_state as $key => $value) {

                $type_approve = Db::table('oa_role')->where(['id' => $value['role']])
                    ->find();
                $result_user = Db::table('oa_user')->field('ch_name')->where(['id' => $value['user_id']])
                    ->find();
                $result['role'] = $type_approve['role'];
                $result['first'] = $value['role_key'];
                $result['apply_states'] = $value['state'];
                $result['user_id'] = $result_user['ch_name'];
                if ($value['state'] == 0) {
                    $result['apply_state'] = '<span style="color: blue">待审批...</span>';
                    $result['audit_time'] = '<span style="color: blue">请等待</span>';
                    $result['audit_name'] = '<span style="color: blue">请等待...</span>';
                    $result['audit_opinion'] = '<span style="color: blue">请等待...</span>';
                }
                if ($value['state'] == 1) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    $result['apply_state'] = '已通过';
                }
                if ($value['state'] == 2) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    $result['apply_state'] = '<span style="color: red">未通过</span>';
                }
                $results[] = $result;
            }
            $flow_id = Db::table('oa_flow')->where(['id' => $res['leave_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow_id'] = $ress;
            //dump($res);die;
            $this->assign('myself_list', $res_list);
            $this->assign('leaves_form', $res);
            $this->assign('leaves_form_state', $results);
            return $this->fetch();
        }
    }

    //我的合同审批修改页面及数据
    public function myselfapply_contract_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            $results = '';
            $id = input('get.id');
            $res = Db::table('oa_contract_form c,oa_region r,oa_contract_type t')
                ->field('r.region,t.contract_type,c.contract_name,c.contract_code,c.contract_party,c.contract_time,c.contract_temporary_price,c.contract_settlement_price,c.contract_description,c.contract_payment_progress,c.contract_approve_time,c.id,c.apply_state,c.contract_flow_id')
                ->where('c.sub_region=r.id and c.contract_type=t.id and c.id=' . $id)->find();
            //dump($res);die;
            //单据相关附件
            $contract_fj = Db::table('oa_contract_enclosure')->where(['contract_form_id' => $id])->select();
            $fj_num['fj_num'] = count($contract_fj);
            $apply_state = json_decode($res['apply_state'], true);
            $apply_state_num = count($apply_state);
            foreach ($apply_state as $key => $value) {

                $type_approve = Db::table('oa_role')->where(['id' => $value['role']])
                    ->find();
                $result_user = Db::table('oa_user')->field('ch_name')->where(['id' => $value['user_id']])
                    ->find();
                $result['role'] = $type_approve['role'];
                $result['first'] = $value['role_key'];
                $result['apply_states'] = $value['state'];
                $result['user_id'] = $result_user['ch_name'];
                if ($value['state'] == 0) {
                    $result['apply_state'] = '<span style="color: blue">待审批...</span>';
                    $result['audit_time'] = '<span style="color: blue">请等待</span>';
                    $result['audit_name'] = '<span style="color: blue">请等待...</span>';
                    $result['audit_opinion'] = '<span style="color: blue">请等待...</span>';
                }
                if ($value['state'] == 1) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    if ($key < $apply_state_num - 1) {
                        $result['apply_state'] = '已通过';
                    } else {
                        $result['apply_state'] = '已转账';
                    }
                }
                if ($value['state'] == 2) {
                    $result['audit_time'] = date('y-m-d h:i:s',$value['audit_time']);
                    $result['audit_name'] = $value['audit_name'];
                    $result['audit_opinion'] = $value['audit_opinion'];
                    $result['apply_state'] = '<span style="color: red">未通过</span>';
                }
                $results[] = $result;
            }
            $expense_reimbursement = Db::table('oa_contract_enclosure')->where(['contract_form_id' => $id])
                ->select();
            $res['contract_enclosure'] = $expense_reimbursement;
            $flow_id = Db::table('oa_flow')->where(['id' => $res['contract_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['contract_flow_id'] = $ress;
            //dump($res);die;
            $this->assign('contract_form', $res);
            $this->assign('contract_form_state', $results);
            $this->assign('contract_form_fj', $contract_fj);
            $this->assign('fj_num', $fj_num);
            return $this->fetch();
        }
    }
    /*------------------申请单据详情页面及数据------------------*/

    //我的费用报销申请修改页面及数据
    public function myselfapprove_expense_reimbursement_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            //单据id
            $id = input('get.id');
            $res = Db::table('oa_expense_reimbursement')->where(['id' => $id])->find();
            //dump($res);die;
            $res['capital_total'] = $this->num_to_rmb($res['total']);
            $approve_state = json_decode($res['apply_state'], true);
            $count = count($approve_state) - 1;
            $type_approve = Db::table('oa_role')->where(['id' => $approve_state[$count]['role']])
                ->find();
            $res['role'] = $type_approve['role'];
            $res['apply_states'] = $approve_state[$count]['state'];
            $res['audit_opinion'] = $approve_state[$count]['audit_opinion'];
            if ($approve_state[$count]['state'] == 0) {
                $res['apply_state'] = '<span style="color: blue">待审批...</span>';
            }
            if ($approve_state[$count]['state'] == 1) {
                $res['apply_state'] = '已通过';
            }
            if ($approve_state[$count]['state'] == 2) {
                $res['apply_state'] = '<span style="color: red">未通过</span>';
            }
            $expense_reimbursement = Db::table('oa_expense_reimbursement_cost')->where(['expense_reimbursement_id' => $id])
                ->select();
            //单据详情的个数
            $num = count($expense_reimbursement);
            if ($num < 5) {
                for ($i = 1; $i <= 5 - $num; $i++) {
                    $expense_reimbursement[$num - 1 + $i]['purpose'] = '';
                    $expense_reimbursement[$num - 1 + $i]['money'] = '';
                }
            }
            $res['expense_reimbursement_cost'] = $expense_reimbursement;
            $flow_id = Db::table('oa_flow')->where(['id' => $res['flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow'] = $ress;
            //dump($res);
            $flow = $this->flow_reimbursement_apply();
            //dump($flow);
            $this->assign('approve', $res);
            $this->assign('flow', $flow);
            return $this->fetch();
        }
    }

    //我的付款申请修改页面及数据
    public function myselfapprove_payment_application_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            //单据id
            $id = input('get.id');
            $res = Db::table('oa_payment_application_form')->where(['id' => $id])->find();
            //dump($res);die;
            $approve_state = json_decode($res['apply_state'], true);
            $count = count($approve_state) - 1;
            $type_approve = Db::table('oa_role')->where(['id' => $approve_state[$count]['role']])
                ->find();
            $res['role'] = $type_approve['role'];
            $res['apply_states'] = $approve_state[$count]['state'];
            $res['audit_opinion'] = $approve_state[$count]['audit_opinion'];
            if ($approve_state[$count]['state'] == 0) {
                $res['apply_state'] = '<span style="color: blue">待审批...</span>';
            }
            if ($approve_state[$count]['state'] == 1) {
                $res['apply_state'] = '已通过';
            }
            if ($approve_state[$count]['state'] == 2) {
                $res['apply_state'] = '<span style="color: red">未通过</span>';
            }
            $flow_id = Db::table('oa_flow')->where(['id' => $res['payment_application_form_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow'] = $ress;
            //dump($res);
            $flow = $this->flow_payment_application_form();
            $this->assign('payment_application_form', $res);
            $this->assign('payment_application_form_flow', $flow);
            return $this->fetch();
        }
    }

    //我的付款申请修改页面及数据
    public function myselfapprove_leaves_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            //单据id
            $id = input('get.id');
            $res = Db::table('oa_leaves_form')->where(['id' => $id])->find();
            //dump($res);die;
            $approve_state = json_decode($res['apply_state'], true);
            $count = count($approve_state) - 1;
            $type_approve = Db::table('oa_role')->where(['id' => $approve_state[$count]['role']])
                ->find();
            $res['role'] = $type_approve['role'];
            $res['apply_states'] = $approve_state[$count]['state'];
            $res['audit_opinion'] = $approve_state[$count]['audit_opinion'];
            if ($approve_state[$count]['state'] == 0) {
                $res['apply_state'] = '<span style="color: blue">待审批...</span>';
            }
            if ($approve_state[$count]['state'] == 1) {
                $res['apply_state'] = '已通过';
            }
            if ($approve_state[$count]['state'] == 2) {
                $res['apply_state'] = '<span style="color: red">未通过</span>';
            }
            $flow_id = Db::table('oa_flow')->where(['id' => $res['leave_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow'] = $ress;
            $res_list = Db::table('oa_user u,oa_branch b')->field('u.ch_name,u.position,b.branch')
                ->where('u.branch_id=b.id and u.id=' . $_SESSION['id'])->find();
            $res['ch_name'] = $res_list['ch_name'];
            $res['branch'] = $res_list['branch'];
            $res['position'] = $res_list['position'];

            //dump($res);
            $flow = $this->flow_leaves();
            //dump($flow);
            $type_leave = Db::table('oa_type_leave')->where('state=1')->select();
            $this->assign('type_leave', $type_leave);
            $this->assign('leaves_form', $res);
            $this->assign('leaves_form_flow', $flow);
            return $this->fetch();
        }
    }

    //我的合同申请修改页面及数据
    public function myselfapprove_contract_form_save()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $ress = '';
            //单据id
            $id = input('get.id');
            $res = Db::table('oa_contract_form')->where(['id' => $id])->find();
            //dump($res);die;
            $approve_state = json_decode($res['apply_state'], true);
            $count = count($approve_state) - 1;
            $type_approve = Db::table('oa_role')->where(['id' => $approve_state[$count]['role']])
                ->find();
            $res['role'] = $type_approve['role'];
            $res['apply_states'] = $approve_state[$count]['state'];
            $res['audit_opinion'] = $approve_state[$count]['audit_opinion'];
            if ($approve_state[$count]['state'] == 0) {
                $res['apply_state'] = '<span style="color: blue">待审批...</span>';
            }
            if ($approve_state[$count]['state'] == 1) {
                $res['apply_state'] = '已通过';
            }
            if ($approve_state[$count]['state'] == 2) {
                $res['apply_state'] = '<span style="color: red">未通过</span>';
            }
            $flow_id = Db::table('oa_flow')->where(['id' => $res['contract_flow_id']])->find();
            //dump($flow_id);die;
            if (!empty($flow_id['first'])) {
                $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
                $ress .= $first['role'];
                //dump($res['flow_id']);die;
            }
            if (!empty($flow_id['second'])) {
                $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
                $ress .= '->' . $second['role'];
            }
            if (!empty($flow_id['third'])) {
                $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
                $ress .= '->' . $third['role'];
            }
            if (!empty($flow_id['fourth'])) {
                $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
                $ress .= '->' . $fourth['role'];
            }
            if (!empty($flow_id['fifth'])) {
                $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
                $ress .= '->' . $fifth['role'];
            }
            if (!empty($flow_id['sixth'])) {
                $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
                $ress .= '->' . $sixth['role'];
            }
            if (!empty($flow_id['seventh'])) {
                $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
                $ress .= '->' . $seventh['role'];
            }
            if (!empty($flow_id['eighth'])) {
                $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
                $ress .= '->' . $eighth['role'];
            }
            if (!empty($flow_id['ninth'])) {
                $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
                $ress .= '->' . $ninth['role'];
            }
            if (!empty($flow_id['tenth'])) {
                $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
                $ress .= '->' . $tenth['role'];
            }
            $res['flow'] = $ress;
            $res_list = Db::table('oa_user u,oa_branch b')->field('u.ch_name,u.position,b.branch')
                ->where('u.branch_id=b.id and u.id=' . $_SESSION['id'])->find();
            $res['ch_name'] = $res_list['ch_name'];
            $res['branch'] = $res_list['branch'];
            $res['position'] = $res_list['position'];

            //dump($res);
            //流程
            $flow = $this->flow_contract_form();
            //dump($flow);
            //合同类型
            $contract_type = Db::table('oa_contract_type')->where('state=1')->select();
            $sub_region = Db::table('oa_region')->where('state=1')->select();
            $this->assign('contract_type', $contract_type);
            $this->assign('sub_region', $sub_region);
            $this->assign('contract_form', $res);
            $this->assign('contract_form_flow', $flow);
            return $this->fetch();
        }
    }



    /*-------------------各种单据被驳回再次申请处理------------------------*/

    //提交我的报销费用申请修改数据及处理（被驳回才会生效）
    public function myselfapprove_expense_reimbursement_saves()
    {
        session_start();
        $nums = 0;
        $purposes = '';
        //return 101;
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            //return 1;
            $session = '';
            $post = input('post.data');
            $res = json_decode($post, true);
            //return $res['id'];
            if (isset($_SESSION['wenjia_name'])) {
                $session = $_SESSION['wenjia_name'];
            }
            //return $res['money_a'];
            $res['bill'] = $session;
            $res['user_id'] = $_SESSION['id'];
            //申请费用要大于0
            if ($res['total'] > 0) {
                //取出审批流程第一级审批的角色
                $first = Db::table('oa_flow')->field('first')->where(['id' => $res['flow_id']])->find();
                //flow键role是将要代表审批的级别，键role_key是审批的角色
                $flow_a['role'] = $first['first'];
                $flow_a['role_key'] = 'first';
                $flow_a['audit_opinion'] = '';
                $flow_a['audit_time'] = '';
                $flow_a['audit_name'] = '';
                $flow_a['state'] = 0;
                //被驳回时的原因
                $flow_a['user_id'] = '';
                $flow[] = $flow_a;
                $res['apply_state'] = json_encode($flow);
                //添加报销信息
                $num = Db::table('oa_expense_reimbursement')->update($res);
                //删除单据详情表全有的单据
                Db::table('oa_expense_reimbursement_cost')->where(['expense_reimbursement_id' => $res['id']])
                    ->delete();
                //处理报销具体的信息
                if ($num) {
                    if (!empty($res['purpose1']) && !empty($res['money1'])) {
                        $purpose['purpose'] = $res['purpose1'];
                        $purpose['money'] = $res['money1'];
                        $purpose['expense_reimbursement_id'] = $res['id'];
                        $purposes[] = $purpose;
                    }
                    if (!empty($res['purpose2']) && !empty($res['money2'])) {
                        $purpose['purpose'] = $res['purpose2'];
                        $purpose['money'] = $res['money2'];
                        $purpose['expense_reimbursement_id'] = $res['id'];
                        $purposes[] = $purpose;
                    }
                    if (!empty($res['purpose3']) && !empty($res['money3'])) {
                        $purpose['purpose'] = $res['purpose3'];
                        $purpose['money'] = $res['money3'];
                        $purpose['expense_reimbursement_id'] = $res['id'];
                        $purposes[] = $purpose;
                    }
                    if (!empty($res['purpose4']) && !empty($res['money4'])) {
                        $purpose['purpose'] = $res['purpose4'];
                        $purpose['money'] = $res['money4'];
                        $purpose['expense_reimbursement_id'] = $res['id'];
                        $purposes[] = $purpose;
                    }
                    if (!empty($res['purpose5']) && !empty($res['money5'])) {
                        $purpose['purpose'] = $res['purpose5'];
                        $purpose['money'] = $res['money5'];
                        $purpose['expense_reimbursement_id'] = $res['id'];
                        $purposes[] = $purpose;
                    }
                    unset($_SESSION['wenjia_name']);
                    //添加报销详情
                    foreach ($purposes as $k => $v) {
                        $num_a = Db::table('oa_expense_reimbursement_cost')->insert($purposes[$k]);
                        $nums = $num_a + 1;
                    }
                    return $nums;
                } else {
                    return $nums;
                }
            } else {
                return $nums;
            }
        } else {
            //return 3;
            return $nums;
        }


    }

    //提交我的付款申请修改数据及处理（被驳回才会生效）
    public function myselfapprove_payment_application_form_saves()
    {
        session_start();
        $nums = 0;
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            $post = input('post.data');
            $res = json_decode($post, true);
            $res['user_id'] = $_SESSION['id'];
            //取出审批流程第一级审批的角色
            $first = Db::table('oa_flow')->field('first')->where(['id' => $res['payment_application_form_flow_id']])->find();
            //flow键role是将要代表审批的级别，键role_key是审批的角色
            $flow_a['role'] = $first['first'];
            $flow_a['role_key'] = 'first';
            $flow_a['audit_opinion'] = '';
            $flow_a['audit_time'] = '';
            $flow_a['audit_name'] = '';
            $flow_a['state'] = 0;
            //被驳回时的原因
            $flow_a['user_id'] = '';
            $flow[] = $flow_a;
            $res['apply_state'] = json_encode($flow);
            //添加报销信息
            $nums = Db::table('oa_payment_application_form')->update($res);
            //处理报销具体的信息
            return $nums;
        } else {
            //return 3;
            return $nums;
        }
    }

    //提交我的付款申请修改数据及处理（被驳回才会生效）
    public function myselfapprove_leaves_form_saves()
    {
        session_start();
        $nums = 0;
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            $post = input('post.data');
            $res = json_decode($post, true);
            //$res['user_id'] = $_SESSION['id'];
            //取出审批流程第一级审批的角色
            //return $res;

            $first = Db::table('oa_flow')->field('first')->where(['id' => $res['leave_flow_id']])->find();
            //return $first;
            //flow键role是将要代表审批的级别，键role_key是审批的角色
            $flow_a['role'] = $first['first'];
            $flow_a['role_key'] = 'first';
            $flow_a['audit_opinion'] = '';
            $flow_a['audit_time'] = '';
            $flow_a['audit_name'] = '';
            $flow_a['state'] = 0;
            $flow_a['user_id'] = '';
            $flow[] = $flow_a;
            $res['apply_state'] = json_encode($flow);
            //添加报销信息
            $nums = Db::table('oa_leaves_form')->update($res);
            //处理报销具体的信息
            return $nums;
        } else {
            //return 3;
            return $nums;
        }
    }

    //提交我的报销费用申请修改数据及处理（被驳回才会生效）
    public function myselfapprove_contract_form_saves()
    {
        session_start();
        //return 101;
        if (isset($_POST['sub']) && $_POST['sub'] == 'submit') {
            //return 1;
            $post = input('post.data');
            $res = json_decode($post, true);
            $res['user_id'] = $_SESSION['id'];
            //取出审批流程第一级审批的角色
            $first = Db::table('oa_flow')->field('first')->where(['id' => $res['contract_flow_id']])->find();
            //flow键role是将要代表审批的级别，键role_key是审批的角色
            $flow_a['role'] = $first['first'];
            $flow_a['role_key'] = 'first';
            $flow_a['audit_opinion'] = '';
            $flow_a['audit_time'] = '';
            $flow_a['audit_name'] = '';
            $flow_a['state'] = 0;
            //被驳回时的原因
            $flow_a['user_id'] = '';
            $flow[] = $flow_a;
            $res['apply_state'] = json_encode($flow);
            //添加报销信息
            $num = Db::table('oa_contract_form')->update($res);
            if ($num) {
                $_SESSION['contract_form_id'] = $res['id'];
            }
            //删除单据详情表全有的单据
            return $num;
        }
    }
    /*------------------------------------*/


    //同意申请进行审批操作处理
    public function apply()
    {
        session_start();
        //return 1;
        if (isset($_POST['data'])) {
            //申请数据的id
            $id = input('post.data');
            $audit_opinion = input('post.audit_opinion');
            if ($audit_opinion == '') {
                $audit_opinion = '无';
            }
            //dump($audit_opinion);
            //return $id;
            //审批用户的id
            $user_id = $_SESSION['id'];
            //用户拥有的角色
            $user_role = Db::table('oa_user_role')->field('role_id')->where(['user_id' => $user_id, 'state' => 1])
                ->select();
            $bill_type_id = input('post.bill_type_id');
            if ($bill_type_id == 1) {
                //申请数据
                $apply_state = Db::table('oa_expense_reimbursement')->field('apply_state,flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['flow_id']])->find();
                //取对应审批流程的审批次数
                $flow_num = count(array_filter($flow));
                //取对应审批流程审批的审批级别（取键名）
                $flow_key = array_keys($flow);
                $flow_key_num = count($flow_key);
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //判断流程的级数不能大于审批的次数
                        if ($flow_num > $count) {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            for ($i = 0; $i < $flow_key_num; $i++) {
                                if ($flow_key[$i] == $apply_state_array[$index]['role_key']) {
                                    $apply_state_array[$count]['role'] = $flow[$flow_key[$i + 1]];
                                    $apply_state_array[$count]['user_id'] = '';
                                    $apply_state_array[$count]['role_key'] = $flow_key[$i + 1];
                                    $apply_state_array[$count]['audit_opinion'] = '';
                                    $apply_state_array[$count]['audit_time'] = '';
                                    $apply_state_array[$count]['audit_name'] = '';
                                    $apply_state_array[$count]['state'] = 0;
                                }
                            }
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        } else {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        }
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 2;
                        }
                    }
                }
            } elseif ($bill_type_id == 2) {
                //申请数据
                $apply_state = Db::table('oa_payment_application_form')->field('apply_state,payment_application_form_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['payment_application_form_flow_id']])->find();
                //取对应审批流程的审批次数
                $flow_num = count(array_filter($flow));
                //取对应审批流程审批的审批级别（取键名）
                $flow_key = array_keys($flow);
                $flow_key_num = count($flow_key);
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //判断流程的级数不能大于审批的次数
                        if ($flow_num > $count) {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            for ($i = 0; $i < $flow_key_num; $i++) {
                                if ($flow_key[$i] == $apply_state_array[$index]['role_key']) {
                                    $apply_state_array[$count]['role'] = $flow[$flow_key[$i + 1]];
                                    $apply_state_array[$count]['user_id'] = '';
                                    $apply_state_array[$count]['role_key'] = $flow_key[$i + 1];
                                    $apply_state_array[$count]['audit_opinion'] = '';
                                    $apply_state_array[$count]['audit_time'] = '';
                                    $apply_state_array[$count]['audit_name'] = '';
                                    $apply_state_array[$count]['state'] = 0;
                                }
                            }
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_payment_application_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        } else {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_payment_application_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        }
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 2;
                        }
                    }
                }
            } elseif ($bill_type_id == 4) {
                //申请数据
                $apply_state = Db::table('oa_leaves_form')->field('apply_state,leave_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['leave_flow_id']])->find();
                //取对应审批流程的审批次数
                $flow_num = count(array_filter($flow));
                //取对应审批流程审批的审批级别（取键名）
                $flow_key = array_keys($flow);
                $flow_key_num = count($flow_key);
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //判断流程的级数不能大于审批的次数
                        if ($flow_num > $count) {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            for ($i = 0; $i < $flow_key_num; $i++) {
                                if ($flow_key[$i] == $apply_state_array[$index]['role_key']) {
                                    $apply_state_array[$count]['role'] = $flow[$flow_key[$i + 1]];
                                    $apply_state_array[$count]['user_id'] = '';
                                    $apply_state_array[$count]['role_key'] = $flow_key[$i + 1];
                                    $apply_state_array[$count]['audit_opinion'] = '';
                                    $apply_state_array[$count]['audit_time'] = '';
                                    $apply_state_array[$count]['audit_name'] = '';
                                    $apply_state_array[$count]['state'] = 0;
                                }
                            }
                            //dump($apply_state);
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_leaves_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        } else {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_leaves_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        }
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 2;
                        }
                    }
                }
            } elseif ($bill_type_id == 8) {
                //申请数据
                $apply_state = Db::table('oa_contract_form')->field('apply_state,contract_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['contract_flow_id']])->find();
                //取对应审批流程的审批次数
                $flow_num = count(array_filter($flow));
                //取对应审批流程审批的审批级别（取键名）
                $flow_key = array_keys($flow);
                $flow_key_num = count($flow_key);
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //判断流程的级数不能大于审批的次数
                        if ($flow_num > $count) {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            for ($i = 0; $i < $flow_key_num; $i++) {
                                if ($flow_key[$i] == $apply_state_array[$index]['role_key']) {
                                    $apply_state_array[$count]['role'] = $flow[$flow_key[$i + 1]];
                                    $apply_state_array[$count]['user_id'] = '';
                                    $apply_state_array[$count]['role_key'] = $flow_key[$i + 1];
                                    $apply_state_array[$count]['audit_opinion'] = '';
                                    $apply_state_array[$count]['audit_time'] = '';
                                    $apply_state_array[$count]['audit_name'] = '';
                                    $apply_state_array[$count]['state'] = 0;
                                }
                            }
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_contract_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        } else {
                            if ($apply_state_array[$index]['state'] == 1) {
                                return 2;
                            }
                            if ($apply_state_array[$index]['state'] == 2) {
                                return 3;
                            }
                            $apply_state_array[$index]['user_id'] = $user_id;
                            $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                            $apply_state_array[$index]['audit_time'] = time();
                            $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                            $apply_state_array[$index]['state'] = 1;
                            $apply_state_json = json_encode($apply_state_array);
                            $apply_state_new['apply_state'] = $apply_state_json;
                            $result = Db::table('oa_contract_form')->where(['id' => $id])->update($apply_state_new);
                            return $result;
                        }
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 2;
                        }
                    }
                }
            }

        }
    }

    //驳回申请进行驳回申请操作处理
    public function reject()
    {
        session_start();
        if (isset($_POST['data'])) {
            $bill_type_id = input('post.bill_type_id');
            //申请数据的id
            //return $bill_type_id;
            $id = input('post.data');
            $audit_opinion=input('post.audit_opinion');
            if ($audit_opinion==''){
                $audit_opinion='无';
            }
            //审批用户的id
            $user_id = $_SESSION['id'];
            //用户拥有的角色
            $user_role = Db::table('oa_user_role')->field('role_id')->where(['user_id' => $user_id, 'state' => 1])
                ->select();
            if ($bill_type_id == 1) {
                //申请数据
                $apply_state = Db::table('oa_expense_reimbursement')->field('apply_state,flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['flow_id']])->find();
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    //判断json串里面的角色与审批级别是否与现在的审批角色与级别一直并且判断用户是否拥有该角色
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //已经审批
                        if ($apply_state_array[$index]['state'] == 1) {
                            return 3;
                        }
                        //已经驳回
                        if ($apply_state_array[$index]['state'] == 2) {
                            return 2;
                        }
                        $apply_state_array[$index]['user_id'] = $user_id;
                        $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                        $apply_state_array[$index]['audit_time'] = time();
                        $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                        $apply_state_array[$index]['state'] = 2;
                        $apply_state_json = json_encode($apply_state_array);
                        $apply_state_new['apply_state'] = $apply_state_json;
                        $result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                        return $result;
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 3;
                        }
                    }
                }
            } elseif ($bill_type_id == 2) {
                //申请数据
                $apply_state = Db::table('oa_payment_application_form')->field('apply_state,payment_application_form_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['payment_application_form_flow_id']])->find();
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    //判断json串里面的角色与审批级别是否与现在的审批角色与级别一直并且判断用户是否拥有该角色
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //已经审批
                        if ($apply_state_array[$index]['state'] == 1) {
                            return 3;
                        }
                        //已经驳回
                        if ($apply_state_array[$index]['state'] == 2) {
                            return 2;
                        }
                        $apply_state_array[$index]['user_id'] = $user_id;
                        $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                        $apply_state_array[$index]['audit_time'] = time();
                        $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                        $apply_state_array[$index]['state'] = 2;
                        $apply_state_json = json_encode($apply_state_array);
                        $apply_state_new['apply_state'] = $apply_state_json;
                        $result = Db::table('oa_payment_application_form')->where(['id' => $id])->update($apply_state_new);
                        return $result;
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 3;
                        }
                    }
                }
            } elseif ($bill_type_id == 4) {
                //return 1;
                //申请数据
                $apply_state = Db::table('oa_leaves_form')->field('apply_state,leave_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['leave_flow_id']])->find();
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    //判断json串里面的角色与审批级别是否与现在的审批角色与级别一直并且判断用户是否拥有该角色
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //已经审批
                        if ($apply_state_array[$index]['state'] == 1) {
                            return 3;
                        }
                        //已经驳回
                        if ($apply_state_array[$index]['state'] == 2) {
                            return 2;
                        }
                        $apply_state_array[$index]['user_id'] = $user_id;
                        $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                        $apply_state_array[$index]['audit_time'] = time();
                        $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                        $apply_state_array[$index]['state'] = 2;
                        $apply_state_json = json_encode($apply_state_array);
                        $apply_state_new['apply_state'] = $apply_state_json;
                        $result = Db::table('oa_leaves_form')->where(['id' => $id])->update($apply_state_new);
                        return $result;
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 3;
                        }
                    }
                }
            } elseif ($bill_type_id == 8) {
                //return 1;
                //申请数据
                $apply_state = Db::table('oa_contract_form')->field('apply_state,contract_flow_id')->where(['id' => $id])
                    ->find();
                //取对应审批流程
                $flow = Db::table('oa_flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
                    ->where(['id' => $apply_state['contract_flow_id']])->find();
                $apply_state_array = json_decode($apply_state['apply_state'], true);
                //取出json串中数组的个数（审批的个数）
                $count = count($apply_state_array);
                //当前审批的下标
                $index = $count - 1;
                foreach ($user_role as $key => $value) {
                    //判断json串里面的角色与审批级别是否与现在的审批角色与级别一直并且判断用户是否拥有该角色
                    if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                        //已经审批
                        if ($apply_state_array[$index]['state'] == 1) {
                            return 3;
                        }
                        //已经驳回
                        if ($apply_state_array[$index]['state'] == 2) {
                            return 2;
                        }
                        $apply_state_array[$index]['user_id'] = $user_id;
                        $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                        $apply_state_array[$index]['audit_time'] = time();
                        $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                        $apply_state_array[$index]['state'] = 2;
                        $apply_state_json = json_encode($apply_state_array);
                        $apply_state_new['apply_state'] = $apply_state_json;
                        $result = Db::table('oa_contract_form')->where(['id' => $id])->update($apply_state_new);
                        return $result;
                    } else {
                        if ($key == (count($user_role) - 1)) {
                            return 3;
                        }
                    }
                }
            }

        }
    }


    /*----------------删除申请--------------------*/
    //删除申请记录（已经审批完的申请）或撤销申请（正在审批的申请）
    public function myselfapprove_del()
    {
        if (isset($_POST['submit']) && $_POST['submit'] == '费用报销申请') {
            //return 1;
            $id = input('post.id');
            $res = Db::table('oa_expense_reimbursement')->field('apply_state')->where(['id' => $id])->find();
            $apply_state = json_decode($res['apply_state'], true);
            $count_apply_state = count($apply_state) - 1;
            //return $count_apply_state;
            if ($apply_state[$count_apply_state]['state'] == 0) {
                $result = Db::table('oa_expense_reimbursement')->delete(['id' => $id]);
                return $result + 2;
            }
            if ($apply_state[$count_apply_state]['state'] == 1 || $apply_state[$count_apply_state]['state'] == 2) {
                $result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update(['state' => 0]);
                return $result;
            }
        } elseif (isset($_POST['submit']) && $_POST['submit'] == '付款申请') {
            $id = input('post.id');
            $res = Db::table('oa_payment_application_form')->field('apply_state')->where(['id' => $id])->find();
            $apply_state = json_decode($res['apply_state'], true);
            $count_apply_state = count($apply_state) - 1;
            //return $count_apply_state;
            if ($apply_state[$count_apply_state]['state'] == 0) {
                $result = Db::table('oa_payment_application_form')->delete(['id' => $id]);
                return $result + 2;
            }
            if ($apply_state[$count_apply_state]['state'] == 1 || $apply_state[$count_apply_state]['state'] == 2) {
                $result = Db::table('oa_payment_application_form')->where(['id' => $id])->update(['state' => 0]);
                return $result;
            }
        } elseif (isset($_POST['submit']) && $_POST['submit'] == '请假申请') {
            $id = input('post.id');
            $res = Db::table('oa_leaves_form')->field('apply_state')->where(['id' => $id])->find();
            $apply_state = json_decode($res['apply_state'], true);
            $count_apply_state = count($apply_state) - 1;
            //return $count_apply_state;
            if ($apply_state[$count_apply_state]['state'] == 0) {
                $result = Db::table('oa_leaves_form')->delete(['id' => $id]);
                return $result + 2;
            }
            if ($apply_state[$count_apply_state]['state'] == 1 || $apply_state[$count_apply_state]['state'] == 2) {
                $result = Db::table('oa_leaves_form')->where(['id' => $id])->update(['state' => 0]);
                return $result;
            }
        } elseif (isset($_POST['submit']) && $_POST['submit'] == '合同申请') {
            $id = input('post.id');
            $res = Db::table('oa_contract_form')->field('apply_state')->where(['id' => $id])->find();
            $apply_state = json_decode($res['apply_state'], true);
            $count_apply_state = count($apply_state) - 1;
            //return $count_apply_state;
            if ($apply_state[$count_apply_state]['state'] == 0) {
                $result = Db::table('oa_contract_form')->delete(['id' => $id]);
                return $result + 2;
            }
            if ($apply_state[$count_apply_state]['state'] == 1 || $apply_state[$count_apply_state]['state'] == 2) {
                $result = Db::table('oa_contract_form')->where(['id' => $id])->update(['state' => 0]);
                return $result;
            }
        }
    }

    //我的申请
    /*public function reimbursement(){
        if ()
    }*/
    //费用报销文件下载
    public function download()
    {
        if (isset($_GET['filepath'])) {
            $filename = $_GET['filepath'];
            $id = input('get.id');
            $expense_reimbursement = Db::table('oa_expense_reimbursement e,oa_user u')->field('e.bills,u.en_name')->where('e.user_id=u.id and e.id=' . $id)
                ->find();
            //dump( $expense_reimbursement);die;
            $path = "http://" . $_SERVER['HTTP_HOST'] . "/oa/public/upload/" . $expense_reimbursement['en_name'] . '/' . $expense_reimbursement['bills'] . '/' . $filename;
            echo file_exists($path);
            $fp = fopen($path, "r");
            $file_size = get_headers($path, true);
            ob_clean();
            //flush();
            //下载文件需要用到的头
            header('Content-Description: File Transfer');
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            //header("Content-Transfer-Encoding: Binary");
            header("Accept-Length:" . $file_size['Content-Length']);
            header("Content-Disposition: attachment; filename=" . $filename);
            header('Expires: 0');//缓存标记为立即过期
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Type: application/force-download");
            $buffer = 1024;
            $file_count = 0;
            // 输出文件内容
            //echo fread($fp,$file_size['Content-Length']);
            //向浏览器返回数据
            while (!feof($fp) && $file_count < $file_size) {
                $file_con = fread($fp, $buffer);
                $file_count += $buffer;
                echo $file_con;
            }
            fclose($fp);
            //修改下载次数
        }


    }

    //合同文件下载
    public function download_contract()
    {
        if (isset($_GET['filepath'])) {
            //$filename = $_GET['filepath'];
            $id = input('get.filepath');
            //dump($id);die;
            $contract_form_fj = Db::table('oa_contract_enclosure')->where(['id' => $id])
                ->find();
            $user_enname_id = Db::table('oa_contract_form')->field('user_id')->where(['id' => $contract_form_fj['contract_form_id']])->find();
            $user_enname = Db::table('oa_user')->field('en_name')->where(['id' => $user_enname_id['user_id']])->find();
            //dump($user_enname);die;
            $path = "http://" . $_SERVER['HTTP_HOST'] . "/oa/public/upload/" . $user_enname['en_name'] . '/' . $contract_form_fj['contract_fj_lujing'] . '/' . $contract_form_fj['contract_fj_name'];
            echo file_exists($path);
            $fp = fopen($path, "r");
            $file_size = get_headers($path, true);
            ob_clean();
            //flush();
            //下载文件需要用到的头
            header('Content-Description: File Transfer');
            //header("Content-type: application/octet-stream");
            //返回的文件类型
            Header("Content-type:application/msword");
            header("Accept-Ranges: bytes");
            //header("Content-Transfer-Encoding: Binary");
            header("Accept-Length:" . $file_size['Content-Length']);
            header("Content-Disposition: attachment; filename=" . $contract_form_fj['contract_fj_name']);
            header('Expires: 0');//缓存标记为立即过期
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Type: application/force-download");
            $buffer = 1024;
            $file_count = 0;
            // 输出文件内容
            //echo fread($fp,$file_size['Content-Length']);
            //向浏览器返回数据
            while (!feof($fp) && $file_count < $file_size) {
                $file_con = fread($fp, $buffer);
                $file_count += $buffer;
                echo $file_con;
            }
            fclose($fp);
            //修改下载次数
        }


    }

    //多文件上传修改合同上传单据
    public function uploads()
    {
        //上传文件
        session_start();
        if (!empty($_SESSION['contract_form_id'])) {
            $username = $_SESSION['en_name'];
            $contract_fj_id = $_SESSION['contract_form_id'];
            $file_name_a = $_FILES['file']['name'];
            $rand = date('His');
            //上传文件目录
            $file_name_b = date('Y-m-d');
            //上传文件名字
            $file_name_c = $rand . '-' . $file_name_a;
            //获取表单上传文件
            $file = request()->file('file');
            if (empty($file)) {
                $arr = ['code' => 0, 'msg' => "格式错误", 'data' => ['src' => "{:url('Apply/upload')}"]];
                return $arr;
            }
            $file_name = iconv('utf-8', 'gb2312', $file_name_c); //利用Iconv函数对文件名进行重新编码
            $name = iconv('utf-8', 'gb2312', ROOT_PATH . 'public/upload/' . $username . '/' . $file_name_b); //利用Iconv函数对文件名进行重新编码

            //移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move($name, $file_name);
            //$name=iconv("gb2312","UTF-8", $info);
            if ($info) {
                //重新上传需要删除原数据
                Db::table('oa_contract_enclosure')->where(['contract_form_id' => $contract_fj_id])->delete();
                //添加新数据
                $res = Db::table('oa_contract_enclosure')->insert(['contract_fj_name' => $file_name_c, 'contract_form_id' => $contract_fj_id, 'contract_fj_lujing' => $file_name_b]);
                if ($res) {
                    $arr = ['code' => 0, 'msg' => "上传成功", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                    //unset($_SESSION['contract_form_id']);
                    return $arr;
                } else {
                    $arr = ['code' => 0, 'msg' => "上传数据失败", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                    return $arr;
                }
            } else {
                //上传失败获取错误信息
                $arr = ['code' => 0, 'msg' => "上传失败", 'data' => ['src' => "{:url('Apply/uploads')}"]];
                return $arr;
            }
        } else {
            $arr = ['code' => 0, 'msg' => "请先提交表单", 'data' => ['src' => "{:url('Apply/uploads')}"]];
            return $arr;
        }

    }


    /*-----------------------Excel------------------*/

    //付款审批转成Excel表格
    public function payment_application_form_excel()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $pz = '';
            $cw = '';
            $id = input('get.id');
            $user_id = $_SESSION['id'];
            $res = Db::table('oa_user u,oa_branch b')->field('b.branch,u.ch_name')->where('u.branch_id=b.id and u.id=' . $user_id)->find();
            $payment_application_form = Db::table('oa_payment_application_form')->where('state=1 and id=' . $id . ' and user_id=' . $user_id)
                ->find();
            $dayin_state = $payment_application_form['apply_state'];
            $dayin_array_state = json_decode($dayin_state, true);
            $num = count($dayin_array_state) - 1;
            if ($dayin_array_state[$num]['state'] == 1) {
                //批准人
                $pz = Db::table('oa_user')->field('ch_name')->where('id=' . $dayin_array_state[$num - 1]['user_id'])
                    ->find();
                //财务
                $cw = Db::table('oa_user')->field('ch_name')->where('id=' . $dayin_array_state[$num]['user_id'])
                    ->find();
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 0) {
                //批准人
                $pz['ch_name'] = "";
                $cw['ch_name'] = "";
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 2) {
                //批准人
                $pz['ch_name'] = "";
                $cw['ch_name'] = "";
                //dump($pz);die;
            }
            $captial = $this->num_to_rmb($payment_application_form['amount_payment']);
            //dump($payment_application_form);die;
            // 引用phpexcel类
            Loader::import('PHPExcel.Classes.PHPExcel');//手动引入PHPExcel.php
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
            //Loader::import('Classes/PHPExcel/Writer/Excel2007.php');
            // 创建对象
            $objPHPExcel = new \PHPExcel();
            // 显示错误信息
            error_reporting(E_ALL);
            // Set properties
            $objPHPExcel->getProperties()->setCreator("admin")
                ->setLastModifiedBy("admin")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");
            // 设置宽度
            //$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
            //$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
            //$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
            //$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(60);
            // 设置行高度
            //$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //设置默认行高
            //$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);    //第一行行高
            //$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);    //第二行行高
            // 字体和样式
            //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12);   //字体大小
            //$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(false); //第二行是否加粗
            //$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);    //第一行是否加粗
            //$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);         //第一行字体大小
            // 设置垂直居中
            //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            //$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //边框样式
            /*$styleArray = array(
                'borders' => array(
                    'allborders' => array(
                        //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                        'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                        'color' => array('argb' => $color),
                    ),
                ),
            );*/
            //$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($styleArray);
            //边框
            //$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('G3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('G4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('G6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('F7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('G7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('H7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            /*$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(300);*/

            // 合并
            $objPHPExcel->getActiveSheet()->mergeCells('A1:H1');
            $objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
            $objPHPExcel->getActiveSheet()->mergeCells('C2:H2');
            $objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
            $objPHPExcel->getActiveSheet()->mergeCells('C3:D3');
            $objPHPExcel->getActiveSheet()->mergeCells('E3:F3');
            $objPHPExcel->getActiveSheet()->mergeCells('G3:H3');
            $objPHPExcel->getActiveSheet()->mergeCells('A4:B4');
            $objPHPExcel->getActiveSheet()->mergeCells('C4:D4');
            $objPHPExcel->getActiveSheet()->mergeCells('E4:F4');
            $objPHPExcel->getActiveSheet()->mergeCells('G4:H4');
            $objPHPExcel->getActiveSheet()->mergeCells('A5:B5');
            $objPHPExcel->getActiveSheet()->mergeCells('C5:F5');
            $objPHPExcel->getActiveSheet()->mergeCells('G5:H5');
            $objPHPExcel->getActiveSheet()->mergeCells('A6:B6');
            $objPHPExcel->getActiveSheet()->mergeCells('C6:D6');
            $objPHPExcel->getActiveSheet()->mergeCells('E6:F6');
            $objPHPExcel->getActiveSheet()->mergeCells('G6:H6');
            //$objPHPExcel->getActiveSheet()->unmergeCells('A7：B7');
            // 表头
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '付款申请单');
            //->setCellValue('A2', '编号');
            // 内容
            $objPHPExcel->getActiveSheet()->setCellValue('A1', "付款申请单");
            $objPHPExcel->getActiveSheet()->setCellValue('A2', '付款事由');
            $objPHPExcel->getActiveSheet()->setCellValue('C2', $payment_application_form['account_payment']);
            $objPHPExcel->getActiveSheet()->setCellValue('A3', '收款单位');
            $objPHPExcel->getActiveSheet()->setCellValue('C3', $payment_application_form['payee']);
            $objPHPExcel->getActiveSheet()->setCellValue('E3', '时间');
            $objPHPExcel->getActiveSheet()->setCellValue('G3', $payment_application_form['payment_application_form_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('A4', '银行账号');
            $objPHPExcel->getActiveSheet()->setCellValue('C4', $payment_application_form['bank_account']);
            $objPHPExcel->getActiveSheet()->setCellValue('E4', '开户银行');
            $objPHPExcel->getActiveSheet()->setCellValue('G4', $payment_application_form['account_opening_bank']);
            $objPHPExcel->getActiveSheet()->setCellValue('A5', '付款金额');
            $objPHPExcel->getActiveSheet()->setCellValue('C5', '（大写）' . $captial);
            $objPHPExcel->getActiveSheet()->setCellValue('G5', '¥：' . $payment_application_form['amount_payment']);
            //$objPHPExcel->getActiveSheet()->setCellValue('H5', '');
            $objPHPExcel->getActiveSheet()->setCellValue('A6', '付款方式');
            $objPHPExcel->getActiveSheet()->setCellValue('C6', $payment_application_form['payment_method']);
            $objPHPExcel->getActiveSheet()->setCellValue('E6', '转账号码');
            $objPHPExcel->getActiveSheet()->setCellValue('G6', $payment_application_form['tranfser_number']);
            $objPHPExcel->getActiveSheet()->setCellValue('A7', '批准人：');
            $objPHPExcel->getActiveSheet()->setCellValue('B7', $pz['ch_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C7', '财务：');
            $objPHPExcel->getActiveSheet()->setCellValue('D7', $cw['ch_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('E7', '申请部门：');
            $objPHPExcel->getActiveSheet()->setCellValue('F7', $res['branch']);
            $objPHPExcel->getActiveSheet()->setCellValue('G7', '经手人：');
            $objPHPExcel->getActiveSheet()->setCellValue('H7', $res['ch_name']);


            //重构内容-文章名称录入
            /*for ($i = 0, $len = count($newData); $i < $len; $i++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $newData[$i]);
                $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                //左对齐
                $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                //边框设置
                //$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                // $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);  //重构行高
                $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->applyFromArray($styleArray);
            }*/

            // //查询出所有行数
            // $all_count=0;
            // foreach ($data as $key => $value) {
            //  if(!empty($value['title'])){
            //      $all_count=$all_count+count($value['title']);
            //  }else{
            //      $all_count++;
            //  }

            //合并对应单元格并录入编号/供稿单位/数量。
            /*$ii=3;//从第三行开始的
            for ($i = 0, $len = count($data); $i < $len; $i++) {
                if(count($data[$i]['title'])>0){
                    //合并 注意-1，因为当前单位有内容且是第一篇
                    $objPHPExcel->getActiveSheet()->mergeCells('A' . ($ii) . ':A' . ($ii-1+count($data[$i]['title']) ));
                    $objPHPExcel->getActiveSheet()->mergeCells('B' . ($ii) . ':B' . ($ii-1+count($data[$i]['title']) ));
                    $objPHPExcel->getActiveSheet()->mergeCells('C' . ($ii) . ':C' . ($ii-1+count($data[$i]['title']) ));

                    //录入记录
                    $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

                    //居中
                    $objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    $ii=$ii-1+count($data[$i]['title']);
                    $ii++;
                }else{
                    //录入记录
                    $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

                    //居中
                    $objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $ii++;
                }
            }*/


            // Rename sheet
            $objPHPExcel->getActiveSheet()->setTitle('付款申请单详情');

            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
            //$objPHPExcel->setActiveSheetIndex(0);
            //ob_end_clean();//清除缓冲区,避免乱码
            $userBrowser = $_SERVER['HTTP_USER_AGENT'];
            $fileName = "付款申请单详情.xls";
            if (preg_match('/MSIE/i', $userBrowser)) {
                $fileName = urlencode($fileName);
            }
            $fileName = iconv('UTF-8', 'GBK//IGNORE', $fileName);
            // 输出
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name="' . $fileName . '"');
            header('Content-Disposition:inline;filename="' . $fileName . '"');
            /*header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . '付款申请单' . '.xls"');
            header('Cache-Control: max-age=0');*/

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }


    }

    //费用报销审批转成Excel表格
    public function expense_reimbursement_excel()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $pz = '';
            $cw = '';
            $id = input('get.id');
            $user_id = $_SESSION['id'];
            $res = Db::table('oa_user u,oa_branch b')->field('b.branch,u.ch_name')->where('u.branch_id=b.id and u.id=' . $user_id)->find();
            $expense_reimbursement = Db::table('oa_expense_reimbursement')->where('state=1 and id=' . $id . ' and user_id=' . $user_id)
                ->find();
            $dayin_state = $expense_reimbursement['apply_state'];
            $dayin_array_state = json_decode($dayin_state, true);
            $num = count($dayin_array_state) - 1;
            if ($dayin_array_state[$num]['state'] == 1) {

                //财务主管
                $cw = Db::table('oa_user')->field('ch_name')->where('id=' . $dayin_array_state[$num]['user_id'])
                    ->find();
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 0) {
                //财务主管
                $cw['ch_name'] = "";
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 2) {
                //财务主管
                $cw['ch_name'] = "";
                //dump($pz);die;
            }
            $captial = $this->num_to_rmb($expense_reimbursement['total']);
            //dump($payment_application_form);die;
            // 引用phpexcel类
            Loader::import('PHPExcel.Classes.PHPExcel');//手动引入PHPExcel.php
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
            // 创建对象
            $objPHPExcel = new \PHPExcel();
            // 显示错误信息
            error_reporting(E_ALL);
            // Set properties
            $objPHPExcel->getProperties()->setCreator("admin")
                ->setLastModifiedBy("admin")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");
            $expense_reimbursement_cost = Db::table('oa_expense_reimbursement_cost')->where('expense_reimbursement_id=' . $expense_reimbursement['id'])
                ->select();
            // 合并
            $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
            $objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
            $objPHPExcel->getActiveSheet()->mergeCells('C2:D2');
            //$objPHPExcel->getActiveSheet()->unmergeCells('A7：B7');
            // 表头
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '费用报销单');
            //->setCellValue('A2', '编号');
            // 内容

            $objPHPExcel->getActiveSheet()->setCellValue('A1', "费用报销单");
            $objPHPExcel->getActiveSheet()->setCellValue('A2', '用途');
            $objPHPExcel->getActiveSheet()->setCellValue('C2', '金额（元）');
            $objPHPExcel->getActiveSheet()->setCellValue('A3', '');
            $objPHPExcel->getActiveSheet()->setCellValue('B3', '');
            $m = 3;
            for ($i = 0; $i < count($expense_reimbursement_cost); $i++) {
                //居中
                $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                //合并
                $objPHPExcel->getActiveSheet()->mergeCells("A" . $m . ":B" . $m);
                $objPHPExcel->getActiveSheet()->mergeCells("C" . $m . ":D" . $m);
                //内容
                $objPHPExcel->getActiveSheet()->setCellValue("A" . $m, $expense_reimbursement_cost[$i]['purpose']);
                $objPHPExcel->getActiveSheet()->setCellValue("C" . $m, $expense_reimbursement_cost[$i]['money']);
                $m++;
            }
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //合并
            $objPHPExcel->getActiveSheet()->mergeCells('A' . $m . ':B' . $m);
            $objPHPExcel->getActiveSheet()->mergeCells('C' . $m . ':D' . $m);
            //内容
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $m, '合计');
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $m, $expense_reimbursement['total']);
//excel下标加一表示下一行
            $m = $m + 1;
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //内容
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $m, '大写金额：');
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $m, $captial);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $m, '时间');
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $m, $expense_reimbursement['reimbursement_time']);


            $m = $m + 1;
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //内容
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $m, '原借款：');
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $m, $expense_reimbursement['original_borrow']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $m, '退/补：');
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $m, $expense_reimbursement['return']);


            $m = $m + 1;
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //内容
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $m, '申请部门');
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $m, $res['branch']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $m, '会计主管');
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $m, $cw['ch_name']);


            $m = $m + 1;
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $m)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 合并
            $objPHPExcel->getActiveSheet()->mergeCells('A' . $m . ':B' . $m);
            $objPHPExcel->getActiveSheet()->mergeCells('C' . $m . ':D' . $m);
            //内容
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $m, '单据及附件页数');
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $m, '');


            //重构内容-文章名称录入
            /*for ($i = 0, $len = count($newData); $i < $len; $i++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $newData[$i]);
                $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                //左对齐
                $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
                //边框设置
                //$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                // $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);  //重构行高
                $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->applyFromArray($styleArray);
            }*/

            // //查询出所有行数
            // $all_count=0;
            // foreach ($data as $key => $value) {
            //  if(!empty($value['title'])){
            //      $all_count=$all_count+count($value['title']);
            //  }else{
            //      $all_count++;
            //  }

            //合并对应单元格并录入编号/供稿单位/数量。
            /*$ii=3;//从第三行开始的
            for ($i = 0, $len = count($data); $i < $len; $i++) {
                if(count($data[$i]['title'])>0){
                    //合并 注意-1，因为当前单位有内容且是第一篇
                    $objPHPExcel->getActiveSheet()->mergeCells('A' . ($ii) . ':A' . ($ii-1+count($data[$i]['title']) ));
                    $objPHPExcel->getActiveSheet()->mergeCells('B' . ($ii) . ':B' . ($ii-1+count($data[$i]['title']) ));
                    $objPHPExcel->getActiveSheet()->mergeCells('C' . ($ii) . ':C' . ($ii-1+count($data[$i]['title']) ));

                    //录入记录
                    $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

                    //居中
                    $objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                    $ii=$ii-1+count($data[$i]['title']);
                    $ii++;
                }else{
                    //录入记录
                    $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
                    $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

                    //居中
                    $objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    $ii++;
                }
            }*/


            // Rename sheet
            $objPHPExcel->getActiveSheet()->setTitle('费用报销单详情');

            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
            //$objPHPExcel->setActiveSheetIndex(0);
            $userBrowser = $_SERVER['HTTP_USER_AGENT'];
            $fileName = "费用报销单详情.xls";
            if (preg_match('/MSIE/i', $userBrowser)) {
                $fileName = urlencode($fileName);
            }
            $fileName = iconv('UTF-8', 'GBK//IGNORE', $fileName);
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name="' . $fileName . '"');
            header('Content-Disposition:inline;filename="' . $fileName . '"');
            // 输出
            /*header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . '费用报销单' . '.xls"');
            header('Cache-Control: max-age=0');*/

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }


    }

    //费用报销审批转成Excel表格
    public function leaves_form_excel()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            $pz = '';
            $cw = '';
            $id = input('get.id');
            $user_id = $_SESSION['id'];
            $res = Db::table('oa_user u,oa_branch b')->field('b.branch,u.ch_name,u.position')->where('u.branch_id=b.id and u.id=' . $user_id)->find();
            $leaves_form = Db::table('oa_leaves_form')->where('state=1 and id=' . $id . ' and user_id=' . $user_id)
                ->find();
            $dayin_state = $leaves_form['apply_state'];
            $dayin_array_state = json_decode($dayin_state, true);
            $num = count($dayin_array_state) - 1;
            if ($dayin_array_state[$num]['state'] == 1) {
                //请假批准人
                $cw = Db::table('oa_user')->field('ch_name')->where('id=' . $dayin_array_state[$num]['user_id'])
                    ->find();
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 0) {
                //请假批准人
                $cw['ch_name'] = "";
            }
            if ($dayin_array_state[$num]['state'] == 2) {
                //请假批准人
                $cw['ch_name'] = "";
            }
            // 引用phpexcel类
            Loader::import('PHPExcel.Classes.PHPExcel');//手动引入PHPExcel.php
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
            // 创建对象
            $objPHPExcel = new \PHPExcel();
            // 显示错误信息
            error_reporting(E_ALL);
            // Set properties
            $objPHPExcel->getProperties()->setCreator("admin")
                ->setLastModifiedBy("admin")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");
            // 表头
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '请假申请单');
            //->setCellValue('A2', '编号');
            // 内容

            $objPHPExcel->getActiveSheet()->setCellValue('A1', "请假申请单");
            $objPHPExcel->getActiveSheet()->setCellValue('A2', '姓名');
            $objPHPExcel->getActiveSheet()->setCellValue('B2', $res['ch_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C2', '部门');
            $objPHPExcel->getActiveSheet()->setCellValue('D2', $res['branch']);
            $objPHPExcel->getActiveSheet()->setCellValue('E2', '岗位职务');
            $objPHPExcel->getActiveSheet()->setCellValue('F2', $res['position']);
            $objPHPExcel->getActiveSheet()->setCellValue('A3', '调休明细');
            $objPHPExcel->getActiveSheet()->setCellValue('C3', $leaves_form['off']);
            $objPHPExcel->getActiveSheet()->setCellValue('A4', '请假调休补充');
            $objPHPExcel->getActiveSheet()->setCellValue('C4', $leaves_form['leave_ask']);
            $objPHPExcel->getActiveSheet()->setCellValue('A5', '工作交接描述');
            $objPHPExcel->getActiveSheet()->setCellValue('C5', $leaves_form['handover']);
            $objPHPExcel->getActiveSheet()->setCellValue('A6', '职务代理人');
            $objPHPExcel->getActiveSheet()->setCellValue('C6', $leaves_form['job_agent']);
            $objPHPExcel->getActiveSheet()->setCellValue('E6', '请调类别');
            $objPHPExcel->getActiveSheet()->setCellValue('F6', $leaves_form['type_leave']);
            $objPHPExcel->getActiveSheet()->setCellValue('A7', '申请时间');
            $objPHPExcel->getActiveSheet()->setCellValue('B7', $leaves_form['leave_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('E7', '审批人');
            $objPHPExcel->getActiveSheet()->setCellValue('F8', $cw['ch_name']);
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('F2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('F6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('E7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('F7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //合并
            $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
            $objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
            $objPHPExcel->getActiveSheet()->mergeCells('C3:F3');
            $objPHPExcel->getActiveSheet()->mergeCells('A4:B4');
            $objPHPExcel->getActiveSheet()->mergeCells('C4:F4');
            $objPHPExcel->getActiveSheet()->mergeCells('A5:B5');
            $objPHPExcel->getActiveSheet()->mergeCells('C5:F5');
            $objPHPExcel->getActiveSheet()->mergeCells('A6:B6');
            $objPHPExcel->getActiveSheet()->mergeCells('C6:D6');
            $objPHPExcel->getActiveSheet()->mergeCells('B7:D7');

            // Rename sheet
            $objPHPExcel->getActiveSheet()->setTitle('请假单详情');
            $userBrowser = $_SERVER['HTTP_USER_AGENT'];
            //var_dump($userBrowser);die;
            $fileName = "请假单详情.xls";
            if (preg_match('/MSIE/i', $userBrowser)) {
                $fileName = urlencode($fileName);
            }
            $fileName = iconv('UTF-8', 'GBK//IGNORE', $fileName);

            // 输出
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name="' . $fileName . '"');
            header('Content-Disposition:inline;filename="' . $fileName . '"');
            /*header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . '请假申请单' . '.xls"');
            header('Cache-Control: max-age=0');*/

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }


    }

    //合同审批转成Excel表格
    public function contract_form_excel()
    {
        $this->baseindex();
        if (isset($_GET['id'])) {
            //$pz = '';
            $cw = '';
            $id = input('get.id');
            $user_id = $_SESSION['id'];
            $res = Db::table('oa_user')->field('ch_name')->where(['id' => $user_id])->find();
            $contract_form = Db::table('oa_contract_form')->where('state=1 and id=' . $id . ' and user_id=' . $user_id)
                ->find();
            $region = Db::table('oa_region')->field('region')->where(['id' => $contract_form['sub_region'], 'state' => 1])
                ->find();
            $type = Db::table('oa_contract_type')->field('contract_type')->where(['id' => $contract_form['contract_type'], 'state' => 1])->find();
            $dayin_state = $contract_form['apply_state'];
            $dayin_array_state = json_decode($dayin_state, true);
            $num = count($dayin_array_state) - 1;
            if ($dayin_array_state[$num]['state'] == 1) {
                //请假批准人
                $cw = Db::table('oa_user')->field('ch_name')->where('id=' . $dayin_array_state[$num]['user_id'])
                    ->find();
                //dump($pz);die;
            }
            if ($dayin_array_state[$num]['state'] == 0) {
                //请假批准人
                $cw['ch_name'] = "";
            }
            if ($dayin_array_state[$num]['state'] == 2) {
                //请假批准人
                $cw['ch_name'] = "";
            }
            // 引用phpexcel类
            Loader::import('PHPExcel.Classes.PHPExcel');//手动引入PHPExcel.php
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
            // 创建对象
            $objPHPExcel = new \PHPExcel();
            // 显示错误信息
            error_reporting(E_ALL);
            // Set properties
            $objPHPExcel->getProperties()->setCreator("admin")
                ->setLastModifiedBy("admin")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");
            // 表头
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '合同详情单');
            //->setCellValue('A2', '编号');
            // 内容

            $objPHPExcel->getActiveSheet()->setCellValue('A1', "合同详情单");
            $objPHPExcel->getActiveSheet()->setCellValue('A2', '申请人');
            $objPHPExcel->getActiveSheet()->setCellValue('B2', $res['ch_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C2', '申请时间');
            $objPHPExcel->getActiveSheet()->setCellValue('D2', $contract_form['contract_approve_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('A3', '提交区域：');
            $objPHPExcel->getActiveSheet()->setCellValue('B3', $region['region']);
            $objPHPExcel->getActiveSheet()->setCellValue('C3', '合同类别：');
            $objPHPExcel->getActiveSheet()->setCellValue('D3', $type['contract_type']);
            $objPHPExcel->getActiveSheet()->setCellValue('A4', '合同名称：');
            $objPHPExcel->getActiveSheet()->setCellValue('B4', $contract_form['contract_name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C4', '合同编号：');
            $objPHPExcel->getActiveSheet()->setCellValue('D4', $contract_form['contract_code']);
            $objPHPExcel->getActiveSheet()->setCellValue('A5', '合同对方：');
            $objPHPExcel->getActiveSheet()->setCellValue('B5', $contract_form['contract_party']);
            $objPHPExcel->getActiveSheet()->setCellValue('C5', '合同签订时间：');
            $objPHPExcel->getActiveSheet()->setCellValue('D5', $contract_form['contract_time']);
            $objPHPExcel->getActiveSheet()->setCellValue('A6', '合同暂定价：');
            $objPHPExcel->getActiveSheet()->setCellValue('B6', $contract_form['contract_temporary_price']);
            $objPHPExcel->getActiveSheet()->setCellValue('C6', '合同结算价：');
            $objPHPExcel->getActiveSheet()->setCellValue('D6', $contract_form['contract_settlement_price']);
            $objPHPExcel->getActiveSheet()->setCellValue('A7', '合同描述：');
            $objPHPExcel->getActiveSheet()->setCellValue('B7', $contract_form['contract_description']);
            $objPHPExcel->getActiveSheet()->setCellValue('A8', '付款进度：');
            $objPHPExcel->getActiveSheet()->setCellValue('B8', $contract_form['contract_payment_progress']);
            // 设置水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D4')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D5')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('B6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('C6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('D6')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //$objPHPExcel->getActiveSheet()->getStyle('B7')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //$objPHPExcel->getActiveSheet()->getStyle('B8')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //合并
            $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
            $objPHPExcel->getActiveSheet()->mergeCells('B7:D7');
            $objPHPExcel->getActiveSheet()->mergeCells('B8:D8');

            // Rename sheet
            $objPHPExcel->getActiveSheet()->setTitle('请假单详情');
            $userBrowser = $_SERVER['HTTP_USER_AGENT'];
            //var_dump($userBrowser);die;
            $fileName = "合同详情.xls";
            if (preg_match('/MSIE/i', $userBrowser)) {
                $fileName = urlencode($fileName);
            }
            $fileName = iconv('UTF-8', 'GBK//IGNORE', $fileName);

            // 输出
            header('Pragma:public');
            header('Content-Type:application/x-msexecl;name="' . $fileName . '"');
            header('Content-Disposition:inline;filename="' . $fileName . '"');
            /*header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . '请假申请单' . '.xls"');
            header('Cache-Control: max-age=0');*/

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }


    }
    //测试的没用
    /*public function myselfapprove_ress()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            $page = input('get.page');
            $num = input('get.limit');
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            //dump($role);die;
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_expense_reimbursement e,oa_type_approve t')
                    ->field('e.id,e.reimbursement_time,e.total,e.apply_state,t.type_approve')
                    ->where('t.id=1 and t.id=e.type_approve')->select();
                //dump($apply_array);
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //dump($v);die;
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        //dump($vv);die;
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = $value['role'] . '：未审批';
                            } elseif ($vv['state'] == 1) {
                                $state = $value['role'] . '：已审批';
                            } else {
                                $state = $value['role'] . '：已驳回';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['state'] = $state;
                            $state_a['reimbursement_time'] = $v['reimbursement_time'];
                            $state_a['type_approve'] = $v['type_approve'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])){
                                $state_a['user_id'] = $vv['user_id'];
                            }else{
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $state_a['total'] = $v['total'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            sort($states);
            $count = count($states);
            dump($states);
            die;
            $data = ['code' => 0, 'msg' => '', 'count' => $count];
            $data['data'] = $states;
            return $data;
        }
    }*/
    public function apply_cs()
    {
        $arr = '[{"role":35,"role_key":"first","state":1,"user_id":1},{"role":1,"user_id":1,"role_key":"second","state":1},{"role":43,"user_id":1,"role_key":"third","state":1},{"role":52,"user_id":1,"role_key":"fourth","state":1},{"role":31,"user_id":1,"role_key":"fifth","state":1},{"role":45,"user_id":1,"role_key":"sixth","state":1},{"role":54,"user_id":1,"role_key":"seventh","state":1},{"role":40,"user_id":1,"role_key":"eighth","state":1},{"role":52,"user_id":1,"role_key":"ninth","state":1},{"role":58,"user_id":1,"role_key":"tenth","state":2}]';
        dump($arr);
        die;
        $this->baseindex();
        $ress = '';
        //单据id
        $id = 8;
        $res = Db::table('oa_expense_reimbursement')->where(['id' => $id])->find();
        //dump($res);die;
        $approve_state = json_decode($res['apply_state'], true);
        $count = count($approve_state) - 1;
        $type_approve = Db::table('oa_role')->where(['id' => $approve_state[$count]['role']])
            ->find();
        $res['role'] = $type_approve['role'];
        $res['apply_states'] = $approve_state[$count]['state'];
        if ($approve_state[$count]['state'] == 0) {
            $res['apply_state'] = '待审批...';
        }
        if ($approve_state[$count]['state'] == 1) {
            $res['apply_state'] = '已通过';
        }
        if ($approve_state[$count]['state'] == 2) {
            $res['apply_state'] = '<span style="color: red">未通过</span>';
        }
        $expense_reimbursement = Db::table('oa_expense_reimbursement_cost')->where(['expense_reimbursement_id' => $id])
            ->select();
        $res['expense_reimbursement_cost'] = $expense_reimbursement;
        $flow_id = Db::table('oa_flow')->where(['id' => $res['flow_id']])->find();
        //dump($flow_id);die;
        if (!empty($flow_id['first'])) {
            $first = Db::table('oa_role')->where('id=' . $flow_id['first'])->find();
            $ress .= $first['role'];
            //dump($res['flow_id']);die;
        }
        if (!empty($flow_id['second'])) {
            $second = Db::table('oa_role')->where('id=' . $flow_id['second'])->find();
            $ress .= '->' . $second['role'];
        }
        if (!empty($flow_id['third'])) {
            $third = Db::table('oa_role')->where('id=' . $flow_id['third'])->find();
            $ress .= '->' . $third['role'];
        }
        if (!empty($flow_id['fourth'])) {
            $fourth = Db::table('oa_role')->where('id=' . $flow_id['fourth'])->find();
            $ress .= '->' . $fourth['role'];
        }
        if (!empty($flow_id['fifth'])) {
            $fifth = Db::table('oa_role')->where('id=' . $flow_id['fifth'])->find();
            $ress .= '->' . $fifth['role'];
        }
        if (!empty($flow_id['sixth'])) {
            $sixth = Db::table('oa_role')->where('id=' . $flow_id['sixth'])->find();
            $ress .= '->' . $sixth['role'];
        }
        if (!empty($flow_id['seventh'])) {
            $seventh = Db::table('oa_role')->where('id=' . $flow_id['seventh'])->find();
            $ress .= '->' . $seventh['role'];
        }
        if (!empty($flow_id['eighth'])) {
            $eighth = Db::table('oa_role')->where('id=' . $flow_id['eighth'])->find();
            $ress .= '->' . $eighth['role'];
        }
        if (!empty($flow_id['ninth'])) {
            $ninth = Db::table('oa_role')->where('id=' . $flow_id['ninth'])->find();
            $ress .= '->' . $ninth['role'];
        }
        if (!empty($flow_id['tenth'])) {
            $tenth = Db::table('oa_role')->where('id=' . $flow_id['tenth'])->find();
            $ress .= '->' . $tenth['role'];
        }
        $res['flow_id'] = $ress;
        $flow = $this->flow();
        $this->assign('approve', $res);
        $this->assign('flow', $flow);
        return $this->fetch();
    }

    public function aq()
    {
        session_start();
        $user_id = $_SESSION['id'];
        $where = 'state = 1 and user_id=' . $user_id;
        //取相关数据
        $res = Db::table('oa_leaves_form')->field('id,type_leave,leave_form_time,bill_type_id,apply_state')
            ->where($where)->order('id DESC')->select();
        foreach ($res as $key => $value) {
            //数据类型（）
            $type_approve = Db::table('oa_type_approve')->field('type_approve')->where(['id' => $value['bill_type_id']])->find();
            $res[$key]['type_approve'] = $type_approve['type_approve'];
            //审批状态（数据库存的json类型）
            $role = json_decode($value['apply_state'], true);
            $count_role_state = count($role) - 1;
            if ($role[$count_role_state]['state'] == 0) {
                $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: #0096e8">审批中...</span>';
            }
            if ($role[$count_role_state]['state'] == 1) {
                $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: black">申请被审批</span>';
            }
            if ($role[$count_role_state]['state'] == 2) {
                $role_res = Db::table('oa_role')->field('role')->where(['id' => $role[$count_role_state]['role']])->find();
                $res[$key]['apply_state'] = '<b>' . $role_res['role'] . '</b>：<span style="color: red">审批被驳回</span>';
            }
        }
        //数据个数
        $count = Db::table('oa_leaves_form')->where('state=1 and user_id=' . $user_id)->count();
        //layui接口格式
        $data = ['code' => 0, 'msg' => '', 'count' => $count];
        $data['data'] = $res;
        dump($data);
    }

    public function aqq()
    {
        session_start();
        //$myselfapply_leaves_form_id=10;
        if (isset($_SESSION['id'])) {
            $where = "t.id=4 and t.id=l.bill_type_id";
            /*if ($myselfapply_leaves_form_id != ''){
                $where.=" and l.id=".$myselfapply_leaves_form_id;
            }*/
            $num = 0;
            $states = '';
            //session_start();
            $user_id = $_SESSION['id'];
            //我拥有的角色
            $role = Db::table('oa_user_role u,oa_role r')->field('u.role_id,r.role')
                ->where('r.id=u.role_id and u.state=1 and  u.user_id=' . $user_id)->select();
            //dump($role);die;
            foreach ($role as $key => $value) {
                //需要该角色审批的数据
                $apply_array = Db::table('oa_leaves_form l,oa_type_approve t')
                    ->field('l.id,l.leave_form_time,l.type_leave,l.apply_state,t.type_approve')
                    ->where($where)->order('l.id DESC')->select();
                // dump($apply_array);die;
                //这是一条数据（表：费用报销申请）
                foreach ($apply_array as $k => $v) {
                    //dump($v);die;
                    //转化审批状态的数据类型（转成数组）
                    $apply = json_decode($v['apply_state'], true);
                    foreach ($apply as $kk => $vv) {
                        //dump($vv);die;
                        if ($vv['role'] == $value['role_id']) {
                            if ($vv['state'] == 0) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: #0096e8">未审批</span>';
                                $num++;
                            } elseif ($vv['state'] == 1) {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: black">已审批</span>';
                            } else {
                                $state = '<b>' . $value['role'] . '</b>：<span style="color: red">已驳回</span>';
                            }
                            $state_a['id'] = $v['id'];
                            $state_a['apply_state'] = $state;
                            $state_a['leave_form_time'] = $v['leave_form_time'];
                            $state_a['type_approve'] = $v['type_approve'];
                            $state_a['type_leave'] = $v['type_leave'];
                            $state_a['role'] = $vv['role'];
                            if (!empty($vv['user_id'])) {
                                $state_a['user_id'] = $vv['user_id'];
                            } else {
                                $state_a['user_id'] = '';
                            }
                            $state_a['role_key'] = $vv['role_key'];
                            $states[] = $state_a;
                        }
                    }
                }
            }
            //$states=sort($states);
            $expense_reimbursement['num'] = $num;
            $expense_reimbursement['leaves_form'] = $states;
            //sort($expense_reimbursement);
            //dump($expense_reimbursement);die;
            dump($expense_reimbursement);
        }
    }

}